var $table = $('#table'), $button = $('#button');
var zTree, rMenu;
$(function() {
	getTree();
});
function getTree() {
	$.ajax({
			url : RS.webPath + '/admin/rsOrgan/queryTree',
			type : 'get',
			dataType : 'json',
			success : function(ret) {
				zTree = $.fn.zTree.init($("#organTree"), setting, ret);
				rMenu = $("#rMenu");
				zTree.expandAll(true);
			}
	});

}
function showRMenu(type, x, y) {
	$("#rMenu ul").show();
	if (type == "root") {
		$("#m_del").hide();
		$("#m_check").hide();
		$("#m_unCheck").hide();
	} else {
		$("#m_del").show();
		$("#m_check").show();
		$("#m_unCheck").show();
	}
	rMenu.css({
			"top" : y + "px",
			"left" : x + "px",
			"visibility" : "visible"
	});

	$("body").bind("mousedown", onBodyMouseDown);
}

function hideRMenu() {
	if (rMenu)
		rMenu.css({
			"visibility" : "hidden"
		});
	$("body").unbind("mousedown", onBodyMouseDown);
}
function onBodyMouseDown(event) {
	if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
		rMenu.css({
			"visibility" : "hidden"
		});
	}
}
var zTree, rMenu;
function showInfo(id) {
	$('#infoIframe').attr('src', RS.webPath + '/admin/rsOrgan/edit?method=UPDATE&id=' + id);
}
// 右键菜单点击事件
function clickMenu(item) {
	hideRMenu();
	var nodes = zTree.getSelectedNodes();
	if (nodes && nodes.length > 0) {
		var id = nodes[0].id;
		if ("new" == item) {
			$('#infoIframe').attr('src', RS.webPath + '/admin/rsOrgan/edit?method=INSERT&pid=' + id);
		} else if ("del" == item) {
			var index = parent.layer.confirm('您确定要删除机构【' + nodes[0].name + '】？', {
				btn : [ '确定', '取消' ]
			}, function() {
				delMenu(id);
				parent.layer.close(index);
				location.reload();
			}, function() {
			});
		} else if ("edit" == item) {
			$('#infoIframe').attr('src', RS.webPath + '/admin/rsOrgan/edit?method=UPDATE&id=' + id);
		}
	}
}

function delMenu(id) {
	var ids = [];
	ids[0] = id;
	$.ajax({
			url : RS.webPath + '/admin/rsOrgan/del',
			type : 'post',
			dataType : 'json',
			data : '__paramSet__=' + JSON.stringify(ids),
			success : function(ret) {
				if (ret.status == 0) {
					parent.layer.alert('删除失败');
				} else {
					parent.layer.alert('删除成功');
				}
			}
	});
}